package com.onepower.core.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onepower.core.domain.mapper.SysMenuInfoMapper;
import com.onepower.core.domain.response.SysMenuInfo;
import com.onepower.core.enums.MenuType;
import com.onepower.core.modules.repository.entity.SysMenu;
import com.onepower.core.modules.repository.entity.extend.SysMenuExtend;
import com.onepower.core.modules.repository.mapper.SysMenuMapper;
import com.onepower.core.modules.service.ISysMenuService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单权限表 服务实现类
 * </p>
 *
 * @author kin
 * @since 2018-08-01
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Override
    public List<SysMenuInfo> getUserMenuList(Integer userId) {
        return parse(getAllMenuList(userId));
    }

    @Override
    public List<SysMenuInfo> getMenuList(boolean showButton) {
        List<SysMenu> menuItemList = baseMapper.selectList(new QueryWrapper<SysMenu>().eq("status", 0));
        List<SysMenuExtend> menuExtendList = SysMenuInfoMapper.MAPPER.converterExtend(menuItemList);

        //查询根菜单列表
        List<SysMenuExtend> parentMenuList = menuExtendList.stream().filter(x -> x.getParentId() == 0).collect(Collectors.toList());
        //递归获取子菜单
        getMenuTreeList(parentMenuList, menuExtendList, showButton);

        return parse(parentMenuList);
    }

    @Override
    public List<String> getPerms(Integer userId) {
        List<SysMenuExtend> menuExtendList = baseMapper.queryUserList(userId);
        return menuExtendList.stream().map(SysMenu::getPerms).collect(Collectors.toList());
    }

    private List<SysMenuInfo> parse(List<SysMenuExtend> menuList) {
        if (CollectionUtils.isEmpty(menuList)) {
            return Collections.emptyList();
        }

        List<SysMenuInfo> list = new ArrayList<>();

        menuList.forEach(x -> {
            SysMenuInfo sysMenuInfo = new SysMenuInfo();
            sysMenuInfo.setId(x.getId());
            sysMenuInfo.setName(x.getMenuName());
            sysMenuInfo.setIcon(x.getIcon());
            sysMenuInfo.setComponent(x.getComponent());
            sysMenuInfo.setType(x.getMenuType());
            sysMenuInfo.setChildren(parse(x.getList()));
            list.add(sysMenuInfo);
        });
        return list;
    }

    /**
     * 获取所有菜单列表
     */
    private List<SysMenuExtend> getAllMenuList(Integer userId) {
        List<SysMenuExtend> menuItemList = baseMapper.queryUserList(userId);

        //查询根菜单列表
        List<SysMenuExtend> parentMenuList = menuItemList.stream().filter(x -> x.getParentId() == 0).collect(Collectors.toList());
        //递归获取子菜单
        getMenuTreeList(parentMenuList, menuItemList, true);

        return parentMenuList;
    }

    /**
     * 递归
     */
    private List<SysMenuExtend> getMenuTreeList(List<SysMenuExtend> menuList, List<SysMenuExtend> menuItemList, boolean showButton) {
        List<SysMenuExtend> subMenuList = new ArrayList<>();

        for (SysMenuExtend entity : menuList) {
            if (entity.getMenuType() == MenuType.BUTTON.getValue() && !showButton) {
                continue;
            }
            List<SysMenuExtend> parentMenuList = menuItemList.stream().filter(x -> Objects.equals(x.getParentId(), entity.getId())).collect(Collectors.toList());
            entity.setList(getMenuTreeList(parentMenuList, menuItemList, showButton));
            subMenuList.add(entity);
        }

        return subMenuList;
    }
}
